<?php
class Administrator_NewsController extends Zend_Controller_Action
{	
    public function init() {
    	include 'phpQuery.php';
        include 'simple_html_dom.php';
    }
    public function indexAction()
    {
      $key                     =  $this->_request->getParam('key',false);
      $order_field                   =  $this->_request->getParam('order',false);
      $type                    =  $this->_request->getParam('type',false);
      $this->view->title = "News";	
    	try {
    	    $where = "1=1 and status <> 'delete'";
            $order = "created desc";
            if($key != NULL){
                $key = $this->stripUnicode($key);
                $where .= " and tag_search LIKE '%{$key}%'";
            }
            if($order_field != NULL){
                $order = "{$order_field} {$type}";
            }
      		$where .= " and ((crawler_id IS NULL) OR (crawler_id IS NOT NULL AND status='active'))";
            $model = new HT_Model_administrator_models_news();
            //$dataList = $model->getAll($where."  order by id_news desc");
            //print_r($model->listall()); die;
            $adapter = new Zend_Paginator_Adapter_DbSelect($model->listall($where, $order));
            
            $paginator = new Zend_Paginator($adapter);
           
            
            $currentPage       = 1;
            //Check if the user is not on page 1
            $page              = $this->_getParam('page');
            if (! empty($page)) { //Where page is the current page
                $currentPage   = $this->_getParam('page');
            }            
            //Set the properties for the pagination
            $paginator->setItemCountPerPage(50);
            $paginator->setPageRange(20);
            $paginator->setCurrentPageNumber($currentPage);
            Zend_Paginator::setDefaultScrollingStyle('Sliding');
            Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
            $paginator->setView($this->view);
            $this->view->data = $paginator;   
            $this->view->order_field = $order_field;
            $this->view->order_type  = $type;
                
         }catch(Exception $ex){
            echo "<pre>";
            print_r($ex->getMessage());
        }
    
    }
	
	/*
	 * Crawler News's List
	 */
	public function crawlerAction() {
      $key                     =  $this->_request->getParam('key',false);
      $order_field                   =  $this->_request->getParam('order',false);
      $type                    =  $this->_request->getParam('type',false);
      $this->view->title = "News";	
    	try {
    	    $where = "1=1 and status <> 'delete'";
            $order = "id_news desc";
            if($key != NULL){
                $key = $this->stripUnicode($key);
                $where .= " and tag_search LIKE '%{$key}%'";
            }
            if($order_field != NULL){
                $order = "{$order_field} {$type}";
            }
      		$where .= "and crawler_id IS NOT NULL and status <> 'active'";
            $model = new HT_Model_administrator_models_news();
            //$dataList = $model->getAll($where."  order by id_news desc");
            //print_r($model->listall()); die;
            $adapter = new Zend_Paginator_Adapter_DbSelect($model->listall($where, $order));
            
            $paginator = new Zend_Paginator($adapter);
           
            
            $currentPage       = 1;
            //Check if the user is not on page 1
            $page              = $this->_getParam('page');
            if (! empty($page)) { //Where page is the current page
                $currentPage   = $this->_getParam('page');
            }            
            //Set the properties for the pagination
            $paginator->setItemCountPerPage(50);
            $paginator->setPageRange(20);
            $paginator->setCurrentPageNumber($currentPage);
            Zend_Paginator::setDefaultScrollingStyle('Sliding');
            Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
            $paginator->setView($this->view);
            $this->view->data = $paginator;   
            $this->view->order_field = $order_field;
            $this->view->order_type  = $type;
                
         }catch(Exception $ex){
            echo "<pre>";
            print_r($ex->getMessage());
        }		
	}
	
    public function addAction() {
       
        try {
           $model = new HT_Model_administrator_models_news();
           $form                   = new Administrator_Form_News();
           if ($this->_request->isPost()) {
                $formData = $this->_request->getPost();
                $formData['tags'] = implode(",", $formData['tags']);
                $formData = $this->_clean($formData);
                if ($form->isValid($formData)) {
                   $dataInsert = $form->getValues();                  
                   unset($dataInsert['news_tag']);
                   unset($dataInsert['category_name']);
				   unset($dataInsert['service']);
                   $dataInsert['tag_search'] = $this->stripUnicode($formData['title_vn'])." ".$this->stripUnicode($formData['desc_vn'])." ".$this->stripUnicode($formData['content_vn']);
				   if(isset($formData['status']) && $formData['status'] == 1) {
						$dataInsert['status'] = "active";
				   } else {
				   		$dataInsert['status'] = "";
				   }
				   $dataInsert['ser_action'] = implode(",", $dataInsert['ser_action']);				   
                   $id = $model->insert($dataInsert);
                   $modelOject = new HT_Model_administrator_models_object();
                   $modelOject->insert(array('object_id'=>$id, 'object_type_id'=>2, 'object_description'=>$dataInsert['title_vn']));
                   if($formData['news_tag'] != NULL){
                    foreach($formData['news_tag'] as $vTag):
                       $modelLinkNewsTag = new HT_Model_administrator_models_linknewstag();
                       $modelLinkNewsTag->insert(array('news_id'=>$id, 'tag_id'=>$vTag));
                    endforeach;
                   }
                 
                   if($formData['category_name'] != NULL){
                    foreach($formData['category_name'] as $vCat):
                       $modelLinkNewsCat = new HT_Model_administrator_models_linknewscategory();
                       $modelLinkNewsCat->insert(array('id_news'=>$id, 'id_category'=>$vCat));
                    endforeach;
                   }
                  
				   //Service data
				   if (isset($formData['service'])) {
					   foreach ($formData['service'] as $key => $value) {
							$dataNews_serphase = array('news_id'=>$id, 'serphase_id'=>$value);
					  		$model->insertInNews_serphase($dataNews_serphase);
					   }
				   }				  
				  
                   $this->_redirect('/administrator/news');
                }
            }
           //set view
           $this->view->form       = $form;
        }catch(Exception $ex){
            echo "<pre>";
            print_r($ex->getMessage());
        }
    }
    public function editAction() {
        try {
           $model = new HT_Model_administrator_models_news();
           $modelLinkNewsCat = new HT_Model_administrator_models_linknewscategory();
           $modelLinkNewsTag = new HT_Model_administrator_models_linknewstag();
           
           $form                   = new Administrator_Form_News();
           $form->getElement('pictrue')->setRequired(false);
           $id                     =  $this->_request->getParam('id',false);
           $data = $model->findById($id);
		   if($data['status'] == "active") $data['status'] = 1;
           $dataCat = $modelLinkNewsCat->getAll("id_news = $id");
           $dataTag = $modelLinkNewsTag->getAll("news_id = $id");
		   $dataService = $model->getNews_serphase("news_id = $id");
           if($dataCat != NULL){
            foreach($dataCat as $vCat):
                $data['category_name'][] = $vCat['id_category'];
            endforeach;
           }
           if($dataTag != NULL){
            foreach($dataTag as $vTag):
                $data['news_tag'][] = $vTag['tag_id'];
            endforeach;
           }
		   if($dataService != NULL){
            foreach($dataService as $vSer):
                $data['service'][] = $vSer['serphase_id'];
            endforeach;		   	
		   }
           $data['tags'] = explode(",", $data['tags']);
		   $data['ser_action'] = explode(",", $data['ser_action']);
           $form->populate($data);
           if ($this->_request->isPost()) {
                $formData = $this->_request->getPost();
			   	if (isset($formData['tags'])) {
                	$formData['tags'] = implode(",", $formData['tags']);
                }
                $formData = $this->_clean($formData);
                if ($form->isValid($formData)) {
                   $dataInsert = $form->getValues();
                   
                   unset($dataInsert['news_tag']);
                   unset($dataInsert['category_name']);
				   unset($dataInsert['service']);
                   $tag_search = $this->stripUnicode($formData['title_vn'])." ".$this->stripUnicode($formData['desc_vn'])." ".$this->stripUnicode($formData['content_vn']);
           			
                   for($i=1;$i<=50;$i++){
                   	$strRreplace = "__note$i".'__';
                   	$tag_search = str_replace($strRreplace,'', $tag_search);
                   }
                   $dataInsert['tag_search'] = $tag_search;
                   
                   $dataInsert['pictrue'] = $dataInsert['pictrue'] == NULL ? $data['pictrue'] : $dataInsert['pictrue'];
				   if(isset($formData['status']) && $formData['status'] == 1) {
						$dataInsert['status'] = "active";
				   } else {
				   		$dataInsert['status'] = "";
				   }
				   $dataInsert['ser_action'] = implode(",", $dataInsert['ser_action']);
                   $model->update($dataInsert, "id_news = $id");
	
                   $modelLinkNewsTag1 = new HT_Model_administrator_models_linknewstag();
                   $modelLinkNewsTag1->delete("news_id = $id"); 
                   if($formData['news_tag'] != NULL){
                    
                    foreach($formData['news_tag'] as $vTag):
                       $modelLinkNewsTag = new HT_Model_administrator_models_linknewstag();
                       $modelLinkNewsTag->insert(array('news_id'=>$id, 'tag_id'=>$vTag));
                    endforeach;
                   }

                   $modelLinkNewsCat1 = new HT_Model_administrator_models_linknewscategory();
                   $modelLinkNewsCat1->delete("id_news = $id");
				   
                   if($formData['category_name'] != NULL){
                    
                    foreach($formData['category_name'] as $vCat):
                       $modelLinkNewsCat = new HT_Model_administrator_models_linknewscategory();
                       $modelLinkNewsCat->insert(array('id_news'=>$id, 'id_category'=>$vCat));
                    endforeach;
                 	}

				   //Service data
				   if (isset($formData['service'])) {
					   $model->delInNews_serphase($id);
					   foreach ($formData['service'] as $key => $value) {
							$dataNews_serphase = array('news_id'=>$id, 'serphase_id'=>$value);
					  		$model->insertInNews_serphase($dataNews_serphase);
					   }
				   }				   
                   $this->_redirect('/administrator/news');
                }
            }
           //set view
           $this->view->form       = $form;
        }catch(Exception $ex){
            echo "<pre>";
            print_r($ex->getMessage());
        }
    }
    public function changestatusAction(){
        $id                     =  $this->_request->getParam('id',false);
        $active                     =  $this->_request->getParam('active',false);

        $model = new HT_Model_administrator_models_comment();
        $model->update(array("status" => $active), "id = $id");
        $this->_redirect($_SERVER['HTTP_REFERER']);
    }
	public function deleteAction() {
         $id                     =  $this->_request->getParam('id',false);
         $model = new HT_Model_administrator_models_news();
         $where = "id_news = $id";
         $model->update(array('status'=>'delete'), $where);
         $this->_redirect($_SERVER['HTTP_REFERER']);
    }
    public function stripUnicode($str) {

        $str = strip_tags($str);
        $str = html_entity_decode($str, ENT_COMPAT, 'utf-8');
		$str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ|À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ|A)/", 'a', $str);
	    $str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ|È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'e', $str);
	    $str = preg_replace("/(ì|í|ị|ỉ|ĩ|Ì|Í|Ị|Ỉ|Ĩ|I)/", 'i', $str);
	    $str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ|Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", 'o', $str);
	    $str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ|Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ|U)/", 'u', $str);
	    $str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ|Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'y', $str);
	    $str = preg_replace("/(đ|Đ|D)/", 'd', $str);
	    $str = preg_replace("/(B)/", 'b', $str);
	    $str = preg_replace("/(C)/", 'c', $str);
	    $str = preg_replace("/(G)/", 'g', $str);
	    $str = preg_replace("/(L)/", 'l', $str);
	    $str = preg_replace("/(M)/", 'm', $str);
	    $str = preg_replace("/(N)/", 'n', $str);
	    $str = preg_replace("/(H)/", 'h', $str);
	    $str = preg_replace("/(T)/", 't', $str);
	    $str = preg_replace("/(K)/", 'k', $str);
	    $str = preg_replace("/(S)/", 's', $str);
	    $str = preg_replace("/(R)/", 'r', $str);
	    $str = preg_replace("/(V)/", 'v', $str);
	    $str = preg_replace("/(Y)/", 'y', $str);
	    $str = preg_replace("/(W)/", 'w', $str);
	    $str = preg_replace("/(P)/", 'p', $str);
	    return $str;
  }
  protected function _clean($value)
    {
        return is_array($value) ? array_map(array($this, '_clean'), $value) : stripslashes($value);
    }
	
	public function getrssnewsAction(){
		
		$mNews = new HT_Model_administrator_models_news();
		
		$row_success = 0;
		$xml = array( 	"http://giadinh.vnexpress.net/rss/bac-sy-tu-van.rss",
						"http://giadinh.vnexpress.net/rss/song-khoe.rss",
						"http://giadinh.vnexpress.net/rss/meo-vat.rss",
						"http://hn.eva.vn/rss/rss.php/85",
						"http://www.camnangthuoc.vn/news/rss.php?mode=view&mode=disease",
						"http://www.camnangthuoc.vn/news/rss.php?mode=view&mode=health",
						"http://www.camnangthuoc.vn/news/rss.php?mode=view&mode=remedy",
						"http://www.camnangthuoc.vn/news/rss.php?mode=view&mode=baby",
						);
		
		echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';						
		for ($j=0; $j < count($xml); $j++) {
			$x = null;
			$xmlDoc = new DOMDocument();
			$xmlDoc->load($xml[$j]);
			if ($xml[$j] == "http://giadinh.vnexpress.net/rss/bac-sy-tu-van.rss" ||
				$xml[$j] == "http://giadinh.vnexpress.net/rss/song-khoe.rss" ||
				$xml[$j] == "http://giadinh.vnexpress.net/rss/meo-vat.rss") {
				$x=$xmlDoc->getElementsByTagName('item');
				
				for ($i=0; $i< $x->length; $i++) {
					$item_title = $x->item($i)->getElementsByTagName('title')
											->item(0)->childNodes->item(0)->nodeValue;
											
					$item_link = $x->item($i)->getElementsByTagName('link')
											->item(0)->childNodes->item(0)->nodeValue;
											
					$item_desc = $x->item($i)->getElementsByTagName('description')
											->item(0)->childNodes->item(0)->nodeValue;
					
					$item_print_link = str_replace("tin-tuc", "print", $item_link);
					$content = file_get_contents ($item_print_link);
					preg_match_all('#<div[^>]*>(.+?)</div>#is', $content, $match);
					$content_crawler = $match[1][4].$match[1][5];
					$content_crawler .= '<p style="text-align:right; font-style:italic; font-family: Arial">(Nguồn VnExpress - wish.vn sưu tầm)</p>';
					$success = 0;
					if(!$mNews->checkExistCrawlerID($item_link)) {
						$content_crawler = $this->crawlerimg($content_crawler,$mNews->getMaxIDNews());
						$data_crawler = array(	'title_vn' => $item_title,
												'content_vn' => $content_crawler,
												'content_mobile_vn' => $content_crawler,
												'status' => "",
												'created' => date("Y-m-d H:i:s"),
												'crawler_id' => $item_link);						
						$success = $mNews->insertCrawlerNews($data_crawler);
					}
					$row_success = $row_success + $success;
				}				
			} else if ($xml[$j] == "http://hn.eva.vn/rss/rss.php/85") {
				$x=$xmlDoc->getElementsByTagName('item');
				if ($x->length > 10) {
					$lenght = 10; 
				} else {
					$lenght = $x->length;
				}
				for ($k=0; $k< $lenght; $k++) {
					
					$item_title = $x->item($k)->getElementsByTagName('title')
											->item(0)->childNodes->item(0)->nodeValue;
											
					$item_link = $x->item($k)->getElementsByTagName('link')
											->item(0)->childNodes->item(0)->nodeValue;
											
					$item_desc = $x->item($k)->getElementsByTagName('description')
											->item(0)->childNodes->item(0)->nodeValue;
											
					$arr = explode("/", $item_link);
					$arr = explode("-", $arr[4]);
					$count = count($arr);
					$arr = $arr[$count-1];
					$arr = preg_replace("(c|.html)", "", $arr);
					$arr = explode("a", $arr);
					
					$link = "http://hn.eva.vn/news/printversion?menu=".$arr[0]."&news=".$arr[1];
					$content = file_get_contents ($link);
					
					preg_match_all ("#<div[^>]*>(.+?)</div>#is", $content, $matches);
					$matches[0][0] = "";
					$matches[0][1] = "";
					$content_crawler = implode(" ", $matches[0]);
					$content_crawler = str_replace('class="left_details"', "", $content_crawler);
					$content_crawler = str_replace('class="right_details"', "", $content_crawler);
					$content_crawler = str_replace('class="txt_sapo_news_details"', "", $content_crawler);
					$content_crawler = str_replace('class="title_sapo_news"', "", $content_crawler);
					$content_crawler = str_replace('class="content_details"', "", $content_crawler);
					$content_crawler .= '<p style="text-align:right; font-style:italic; font-family: Arial">(Nguồn EVA.VN - wish.vn sưu tầm)</p>';
					
					$success = 0;								
					if($mNews->checkExistCrawlerID($item_link) == 0) {
						$content_crawler = $this->crawlerimg($content_crawler,$mNews->getMaxIDNews());
						$data_crawler = array(	'title_vn' => $item_title,
												'content_vn' => $content_crawler,
												'content_mobile_vn' => $content_crawler,
												'status' => "",
												'created' => date("Y-m-d H:i:s"),
												'crawler_id'  => $item_link);
						$success = $mNews->insertCrawlerNews($data_crawler);
					}
					$row_success = $row_success + $success;						
				}				
			} elseif (	$xml[$j] == "http://www.camnangthuoc.vn/news/rss.php?mode=view&mode=disease" || 
						$xml[$j] == "http://www.camnangthuoc.vn/news/rss.php?mode=view&mode=health" || 
						$xml[$j] == "http://www.camnangthuoc.vn/news/rss.php?mode=view&mode=remedy" ||
						$xml[$j] == "http://www.camnangthuoc.vn/news/rss.php?mode=view&mode=baby") {
				$x=$xmlDoc->getElementsByTagName('item');
				if ($x->length > 10) {
					$lenght = 10; 
				} else {
					$lenght = $x->length;
				}
				for ($k=0; $k< $lenght; $k++) {
					
					$item_title = $x->item($k)->getElementsByTagName('title')
											->item(0)->childNodes->item(0)->nodeValue;
											
					$item_link = $x->item($k)->getElementsByTagName('link')
											->item(0)->childNodes->item(0)->nodeValue;
											
					$item_desc = $x->item($k)->getElementsByTagName('description')
											->item(0)->childNodes->item(0)->nodeValue;
											
					$link = $item_link."&mode=print";
					$content = file_get_contents ($link);
					$search = array('<td valign="bottom"><font class="SiteName">Cam nang thuoc danh cho moi nguoi</font></td><td align="right" valign="bottom">http://www.camnangthuoc.vn/news/</td>',
									'&copy; 2008 by Cam nang thuoc danh cho moi nguoi',
									'<link type="text/css" rel="stylesheet" href="vip_style.css">',
									'<link href="vip_style.css" rel="stylesheet" type="text/css">',);
					$replace = "";
					$content = str_replace($search, $replace, $content);
					$pattern= array("#<font class=\"dateTime\">(.*)<\/font>#",
									"#<font class=\"bTitle\">(.*)<\/font>#");
					$content_crawler = preg_replace($pattern,array("",""),$content);
					preg_match_all ("#<body>(.+?)</body>#is", $content_crawler, $matches);
					$content_crawler = $matches[1][0];
					$content_crawler .= '<p style="text-align:right; font-style:italic; font-family: Arial">(Nguồn camnangthuoc.vn - wish.vn sưu tầm)</p>';					
					$success = 0;								
					if($mNews->checkExistCrawlerID($item_link) == 0) {
						$content_crawler = $this->crawlerimg($content_crawler,$mNews->getMaxIDNews());
						$data_crawler = array(	'title_vn' => $item_title,
												'content_vn' => $content_crawler,
												'content_mobile_vn' => $content_crawler,
												'status' => "",
												'created' => date("Y-m-d H:i:s"),
												'crawler_id'  => $item_link);
						$success = $mNews->insertCrawlerNews($data_crawler);
					}
					$row_success = $row_success + $success;						
				}
			} 				
		}
		$_SESSION['msg_alert'] = "Có $row_success tin bài được crawler thành công";						
		$this->_redirect(WEB_PATH.'/administrator/news/crawler');
	}

	public function getcrawlersitecontentAction() {
		$mNews = new HT_Model_administrator_models_news();
		echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
		if ($this->_request->isPost()) {
			$row_success = 0;
				
			//$site_link = "http://www.cimsi.org.vn/CIMSI.aspx?action=Content&MenuChildID=107&type=3";
			$site_name = $this->_request->getParam("site_name");
			$site_link = $this->_request->getParam("url");
			if ($site_name == "www.cimsi.org.vn") {
				$content = file_get_contents($site_link);
				$all_link = $this->_get_links($content);
				//print_r($all_link);
				foreach ($all_link[1] as $key => $value) {
					$tempA[0][] = $value;
				}
				foreach ($all_link[2] as $key => $value) {
					$tempA[1][] = $value;
				}		
				
				for ($i=0; $i < count($tempA[0]); $i++) {
					$content_new = null;
					$item_link = "http://www.cimsi.org.vn/CIMSI.aspx".$tempA[0][$i];
					$item_title = $tempA[1][$i];
					$id_new = explode("=", $tempA[0][$i]);
					$link = "http://www.cimsi.org.vn/PreviewContent.aspx?id=".$id_new[3];
					$content = file_get_contents ($link);
					preg_match_all ("#<div[^>]*>(.+?)</div>#is", $content, $matches);
					$need_replace1 = '<img src="images/btn-print.gif" border="0px" id="IMG1" alt="In trang" />';
					$need_replace2 = '<img height="38" alt="" src="http://www.cimsi.org.vn/images/fckUpload/image/HinhHanh/CIMSI-Thongtinvicuocsong.png" width="150" />';
					$content_new = str_replace($need_replace1, "", implode("", $matches[0]));
					$content_new = str_replace($need_replace2, "", $content_new);
					$pattern= "#<span id=\"lblNgayDuyet\">(.*)<\/span>#";
					$content_crawler = preg_replace($pattern,"",$content_new);	
					$content_crawler .= '<p style="text-align:right; font-style:italic; font-family: Arial">(Nguồn cimsi.org.vn - wish.vn sưu tầm)</p>';
					$success = 0;	
												
					if($mNews->checkExistCrawlerID($item_link) == 0) {
						$content_crawler = $this->crawlerimg($content_crawler, $mNews->getMaxIDNews());
						$data_crawler = array(	'title_vn' => $item_title,
												'content_vn' => $content_crawler,
												'content_mobile_vn' => $content_crawler,
												'status' => "",
												'created' => date("Y-m-d H:i:s"),
												'crawler_id'  => $item_link);
						$success = $mNews->insertCrawlerNews($data_crawler);
					}
					$row_success = $row_success + $success;							
				}
			} elseif ($site_name == "suckhoedoisong.vn") {
				$content = file_get_contents($site_link);
				$all_link = $this->_get_class($content);
				

				$all_link = $this->_get_links2($all_link[1][0]);

				for ($i=0; $i < 11; $i++) { 
					$news_links[] = array('id' => $all_link[1][$i], 'url' => 'http://suckhoedoisong.vn'.$all_link[1][$i], 'title' =>$all_link[2][$i]);
				}
				for ($i=0; $i < 11; $i++) { 
					$news_links[$i]['id'] = explode('/', $news_links[$i]['id']);
					$news_links[$i]['id'] = explode('p', $news_links[$i]['id'][1]);
					$news_links[$i]['id'] = $news_links[$i]['id'][0];
				}
				foreach ($news_links as $key => $value) {
					$content = file_get_contents("http://suckhoedoisong.vn/Print.aspx?NewsID=".$value['id']);
					$doc = phpQuery::newDocumentHTML($content);
					phpQuery::selectDocument($doc);
					$content_crawler = pq('div.detail-cont')->html();
					$content_crawler .= '<p style="text-align:right; font-style:italic; font-family: Arial">(Nguồn suckhoedoisong.vn - wish.vn sưu tầm)</p>';
					$item_title = html_entity_decode($value['title']) ;
					$item_link = $value['url'];
					echo $item_title;
					echo $item_link;
					echo $content_crawler;
					$success = 0;
					if($mNews->checkExistCrawlerID($item_link) == 0) {
						$content_crawler = $this->crawlerimg($content_crawler,$mNews->getMaxIDNews());
						$data_crawler = array(	'title_vn' => $item_title,
												'content_vn' => $content_crawler,
												'content_mobile_vn' => $content_crawler,
												'status' => "",
												'created' => date("Y-m-d H:i:s"),
												'crawler_id'  => $item_link);
						$success = $mNews->insertCrawlerNews($data_crawler);
					}
					$row_success = $row_success + $success;					
				}
			
			} else {
				
			}
			
			$_SESSION['msg_alert'] = "Có $row_success tin bài được crawler thành công";						
			$this->_redirect(WEB_PATH.'/administrator/news/crawler');			
		} else {
			
		}
	}
	
	public function getrssnewsv2Action(){
		$mNews = new HT_Model_administrator_models_news();
		$xmlDoc = new DOMDocument();		
		echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
		if($this->_request->isPost()){
			$site_name = $this->_request->getParam("site_name");
			switch ($site_name) {
				case 'http://www.camnangthuoc.vn/':
					$xml_rss = array("http://www.camnangthuoc.vn/news/rss.php?mode=view&mode=disease",
							 		 "http://www.camnangthuoc.vn/news/rss.php?mode=view&mode=health",
									 "http://www.camnangthuoc.vn/news/rss.php?mode=view&mode=remedy",
									 "http://www.camnangthuoc.vn/news/rss.php?mode=view&mode=baby");

					break;
				case 'http://hn.eva.vn/':
					$xml_rss = array("http://hn.eva.vn/rss/rss.php/85");
					break;
				case 'http://giadinh.vnexpress.net/':
					$xml_rss = array("http://giadinh.vnexpress.net/rss/bac-sy-tu-van.rss",
									 "http://giadinh.vnexpress.net/rss/song-khoe.rss",
									 "http://giadinh.vnexpress.net/rss/meo-vat.rss",);
					break;
				case 'http://afamily.vn/':
					$xml_rss = array("http://afamily.vn/suc-khoe.rss",
									 "http://afamily.vn/me-va-be.rss");
					break;
				case 'http://phunuonline.com.vn/':
					$xml_rss = array("http://phunuonline.com.vn/rss/cate/dinh-duong.html",
									 "http://phunuonline.com.vn/rss/cate/gia-dinh.html");
					break;															
				default:
					$xml_rss = array();
					break;
			}
			if (!empty($xml_rss)) {
				$rss_news = $this->_getRssDetail($xml_rss);
				$this->_saveContentNews($mNews, $rss_news, $site_name);				
			}
			$this->_redirect(WEB_PATH.'/administrator/news/crawler');	
		}
	}

	public function getcrawlersitecontentv2Action(){
		$mNews = new HT_Model_administrator_models_news();
		echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
		$site_name = $this->_request->getParam("site_name");
		$site_link = $this->_request->getParam("url");
		$success = 0;
		$row_success = 0;	
		if ($this->_request->isPost()) {
			switch ($site_name) {
				case 'www.cimsi.org.vn':
					$content = file_get_contents($site_link);
					$all_link = $this->_get_links($content);
					foreach ($all_link[1] as $key => $value) {
						$tempA[0][] = $value;
					}
					foreach ($all_link[2] as $key => $value) {
						$tempA[1][] = $value;
					}
					for ($i=0; $i < count($tempA[0]); $i++) {
						$site_info['item_link'] = $tempA[0][$i];
						$site_info['item_title'] = $tempA[1][$i];
						$item_link = "http://www.cimsi.org.vn/CIMSI.aspx".$tempA[0][$i];
						
						if($mNews->checkExistCrawlerID($item_link) == 0) {
							$content_crawler = $this->_crawlerContent($site_info,$site_name);
							$content_crawler = $this->_crawlerImg($content_crawler,$mNews->getMaxIDNews());
							$data_crawler = array(	'title_vn' => $site_info['item_title'],
													'content_vn' => $content_crawler,
													'content_mobile_vn' => $content_crawler,
													'status' => "",
													'created' => date("Y-m-d H:i:s"),
													'crawler_id'  => $item_link);
							echo $content_crawler;
							die;					
							$success = $mNews->insertCrawlerNews($data_crawler);
							$row_success = $row_success + $success;
						}						
					}										
					break;
				case 'suckhoedoisong.vn':
					$html = file_get_html($site_link);
					$category_content = $html->find('div.tinnb', 0).$html->find('div.w400', 0)->find('.list', 0);
					$html = str_get_html($category_content);
					foreach($html->find('a') as $element){
						if (!empty($element->title)) {
							$category_link[] = array('item_title' => $element->title, 'item_link'=>'http://suckhoedoisong.vn'.$element->href);
						}
					}
					foreach ($category_link as $key => $value) {
						
						if($mNews->checkExistCrawlerID($value['item_link']) == 0) {
							
							$content_crawler = $this->_crawlerContent($value,$site_name);

							$content_crawler = $this->_crawlerImg($content_crawler,$mNews->getMaxIDNews());	
												
							$data_crawler = array(	'title_vn' => html_entity_decode($value['item_title'], ENT_COMPAT, 'UTF-8'),
													'content_vn' => $content_crawler,
													'content_mobile_vn' => $content_crawler,
													'status' => "",
													'created' => date("Y-m-d H:i:s"),
													'crawler_id'  => $value['item_link']);					
							$success = $mNews->insertCrawlerNews($data_crawler);
							$row_success = $row_success + $success;				
						}		
					}
					break;				
				default:
					
					break;
			}
			$_SESSION['msg_alert'] = "Có $row_success tin bài được crawler thành công";	
			$this->_redirect(WEB_PATH.'/administrator/news/crawler');
		}		
	}
	
	/**
	 * Lấy ảnh trên url save về trên host của mình
	 * @param $content_html string (html format)
	 * @param $site string
	 * @return $content_html (string) (content site formated) 
	 */
	public function crawlerimg($content_html,$site){
		preg_match_all('/<img.+src=\"([^\"]+)\"[^\/>]+[\/]?>/i', $content_html, $images); 
		$image_source = $images[1];
		for ($i=0; $i < count($image_source); $i++) { 
			$file_name = $site."_".time()."_".$i;
			$path = '/public/crawler/'.$file_name.'.jpg';
			$handle = fopen(ROOT_PATH.$path, 'x+');
			copy($image_source[$i], ROOT_PATH.$path);
			fclose($handle);
			$content_html =str_replace($image_source[$i],WEB_PATH.$path,$content_html);
		}
		return $content_html;
	}
	
	public function _crawlerImg($content_html,$site){
		$html = str_get_html($content_html);
		$i=0;
		foreach($html->find('img') as $element){
			$file_name = $site."_".time()."_".$i;
			$path = '/public/crawler/'.$file_name.'.jpg';
			$handle = fopen(ROOT_PATH.$path, 'x+');
			copy($element->src, ROOT_PATH.$path);
			fclose($handle);
			$content_html =str_replace($element->src,WEB_PATH.$path,$content_html);
			$i++;			
		}
		return $content_html;		
	}
	
	/**
	 * Lấy nội dung của site sau khi trải qua bước clear các phần thừa của nội dung
	 * @param $site_info array(item_title,item_link,item_desc) 
	 * @param $site string
	 * @return $content_html (string) (content site formated)  
	 */
	public function _crawlerContent($site_info, $site){
		$content_crawler = '';
		switch ($site) {
			//get RSS site
			case 'http://www.camnangthuoc.vn/':
				$link = $site_info['item_link']."&mode=print";
				$content = file_get_contents ($link);
				$search = array('<td valign="bottom"><font class="SiteName">Cam nang thuoc danh cho moi nguoi</font></td><td align="right" valign="bottom">http://www.camnangthuoc.vn/news/</td>',
								'&copy; 2008 by Cam nang thuoc danh cho moi nguoi',
								'<link type="text/css" rel="stylesheet" href="vip_style.css">',
								'<link href="vip_style.css" rel="stylesheet" type="text/css">',);
				$replace = "";
				$content = str_replace($search, $replace, $content);
				$pattern= array("#<font class=\"dateTime\">(.*)<\/font>#",
								"#<font class=\"bTitle\">(.*)<\/font>#");
				$content_crawler = preg_replace($pattern,array("",""),$content);
				preg_match_all ("#<body>(.+?)</body>#is", $content_crawler, $matches);
				$content_crawler = $matches[1][0];
				$content_crawler .= '<p style="text-align:right; font-style:italic; font-family: Arial">(Nguồn camnangthuoc.vn - wish.vn sưu tầm)</p>';
				break;
			case 'http://hn.eva.vn/':
				$arr = explode("/", $site_info['item_link']);
				$arr = explode("-", $arr[4]);
				$count = count($arr);
				$arr = $arr[$count-1];
				$arr = preg_replace("(c|.html)", "", $arr);
				$arr = explode("a", $arr);
				
				$link = "http://hn.eva.vn/news/printversion?menu=".$arr[0]."&news=".$arr[1];
				$content = file_get_contents ($link);
				
				preg_match_all ("#<div[^>]*>(.+?)</div>#is", $content, $matches);
				$matches[0][0] = "";
				$matches[0][1] = "";
				$content_crawler = implode(" ", $matches[0]);
				$content_crawler = str_replace('class="left_details"', "", $content_crawler);
				$content_crawler = str_replace('class="right_details"', "", $content_crawler);
				$content_crawler = str_replace('class="txt_sapo_news_details"', "", $content_crawler);
				$content_crawler = str_replace('class="title_sapo_news"', "", $content_crawler);
				$content_crawler = str_replace('class="content_details"', "", $content_crawler);
				$content_crawler .= '<p style="text-align:right; font-style:italic; font-family: Arial">(Nguồn EVA.VN - wish.vn sưu tầm)</p>';				
				break;
			case 'http://giadinh.vnexpress.net/':
				$item_print_link = str_replace("tin-tuc", "print", $site_info['item_link']);
				$content = file_get_contents ($item_print_link);
				preg_match_all('#<div[^>]*>(.+?)</div>#is', $content, $match);
				$content_crawler = $match[1][4].$match[1][5];
				$content_crawler .= '<p style="text-align:right; font-style:italic; font-family: Arial">(Nguồn VnExpress - wish.vn sưu tầm)</p>';				
				break;
			case 'http://afamily.vn/':
				$link = str_replace(" ", "", $site_info['item_link']);
				$html = file_get_html($link);
				$content_crawler =  $html->find('div.detail_content', 0);
				$content_crawler .= '<p style="text-align:right; font-style:italic; font-family: Arial">(Nguồn afamily.vn - wish.vn sưu tầm)</p>';
				break;
			case 'http://phunuonline.com.vn/':
				$link = str_replace(" ", "", $site_info['item_link']);
				$html = file_get_html($link);
				$content_crawler =  $html->find('div[id=newscontents]', 0);
				$content_crawler .= '<p style="text-align:right; font-style:italic; font-family: Arial">(Nguồn phunuonline.com.vn - wish.vn sưu tầm)</p>';
				break;
			//End get RSS site;
			
			//Get content site
			case 'www.cimsi.org.vn':
				$content_new = null;
				//$item_link = "http://www.cimsi.org.vn/CIMSI.aspx".$tempA[0][$i];
				$item_link = "http://www.cimsi.org.vn/CIMSI.aspx".$site_info['item_link'];
				//$item_title = $tempA[1][$i];
				$item_title = $site_info['item_title'];
				$id_new = explode("=", $site_info['item_link']);
				$link = "http://www.cimsi.org.vn/PreviewContent.aspx?id=".$id_new[3];
				$content = file_get_contents ($link);
				preg_match_all ("#<div[^>]*>(.+?)</div>#is", $content, $matches);
				$need_replace1 = '<img src="images/btn-print.gif" border="0px" id="IMG1" alt="In trang" />';
				$need_replace2 = '<img height="38" alt="" src="http://www.cimsi.org.vn/images/fckUpload/image/HinhHanh/CIMSI-Thongtinvicuocsong.png" width="150" />';
				$content_new = str_replace($need_replace1, "", implode("", $matches[0]));
				$content_new = str_replace($need_replace2, "", $content_new);
				$pattern= "#<span id=\"lblNgayDuyet\">(.*)<\/span>#";
				$content_crawler = preg_replace($pattern,"",$content_new);	
				$content_crawler .= '<p style="text-align:right; font-style:italic; font-family: Arial">(Nguồn cimsi.org.vn - wish.vn sưu tầm)</p>';
				break;
			case 'suckhoedoisong.vn':
				$link = $site_info['item_link'];
				$html = file_get_html($link);
				$content_crawler = $html->find('div.content-news',0);
				$content_crawler .= '<p style="text-align:right; font-style:italic; font-family: Arial">(Nguồn suckhoedoisong.vn - wish.vn sưu tầm)</p>';
				break;
			//End get content site;
			default:
				
				break;
		}
		return $content_crawler;
	}
	
	/**
	 * Lấy danh sách tin RSS từ RSS url
	 * @param $xml_rss string (RSS url)
	 * @return $rss_news array([item_title],[item_link],[item description])
	 */
	public function _getRssDetail($xml_rss){
		$mNews = new HT_Model_administrator_models_news();
		$xmlDoc = new DOMDocument();
		$rss_news = array();
		$count_rss = count($xml_rss);
		$count = 0;
		for ($i=0; $i < $count_rss; $i++) { 
			$xmlDoc->load($xml_rss[$i]);
			$x = $xmlDoc->getElementsByTagName('item');
			if ($x->length > 10) {
				$lenght = 10; 
			} else {
				$lenght = $x->length;
			}
			for ($k=0; $k< $lenght; $k++) {
				$item_title = $x->item($k)->getElementsByTagName('title')
										->item(0)->childNodes->item(0)->nodeValue;
										
				$item_link = $x->item($k)->getElementsByTagName('link')
										->item(0)->childNodes->item(0)->nodeValue;
										
				$item_desc = $x->item($k)->getElementsByTagName('description')
										->item(0)->childNodes->item(0)->nodeValue;										
				
				$rss_news[$count]['item_title'] = $item_title;
				$rss_news[$count]['item_link'] = $item_link; 
				$rss_news[$count]['item_desc'] = $item_link;
				$count++;
			}
		}
	
		return $rss_news;
	}
	
	/**
	 * Lấy dữ liệu tin get về và insert vào database 
	 * @param $mNews HT_Model_administrator_models_news
	 * @param $rss_news array
	 * @param $site_name string
	 */
	public function _saveContentNews($mNews, $rss_news, $site_name){
		$success = 0;
		$row_success = 0;
		if(!empty($rss_news)){
			foreach ($rss_news as $key => $value) {
				if($mNews->checkExistCrawlerID($value['item_link']) == 0) {
					$content_crawler = $this->_crawlerContent($value, $site_name);
					$content_crawler = $this->_crawlerImg($content_crawler,$mNews->getMaxIDNews());
					$data_crawler = array(	'title_vn' => $value['item_title'],
											'content_vn' => $content_crawler,
											'content_mobile_vn' => $content_crawler,
											'status' => "",
											'created' => date("Y-m-d H:i:s"),
											'crawler_id'  => $value['item_link']);					
					$success = $mNews->insertCrawlerNews($data_crawler);
					$row_success = $row_success + $success;
				}								
			}			
		}
		$_SESSION['msg_alert'] = "Có $row_success tin bài được crawler thành công";					
	}
	
	public function testcrawlerAction() {
		echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
		$content = file_get_contents("http://www.cimsi.org.vn/CIMSI.aspx?action=Content&MenuChildID=107&type=3");
		$all_link = $this->_get_links($content);
		//print_r($all_link);
		foreach ($all_link[1] as $key => $value) {
			$tempA[0][] = $value;
		}
		foreach ($all_link[2] as $key => $value) {
			$tempA[1][] = $value;
		}		
		
		for ($i=0; $i < count($tempA[0]); $i++) {
			$content_new = null;
			$item_link = "http://www.cimsi.org.vn/CIMSI.aspx".$tempA[0][$i];
			$item_title = $tempA[1][$i];
			$id_new = explode("=", $tempA[0][$i]);
			$link = "http://www.cimsi.org.vn/PreviewContent.aspx?id=".$id_new[3];
			$content = file_get_contents ($link);
			preg_match_all ("#<div[^>]*>(.+?)</div>#is", $content, $matches);
			$need_replace1 = '<img src="images/btn-print.gif" border="0px" id="IMG1" alt="In trang" />';
			$need_replace2 = '<img height="38" alt="" src="http://www.cimsi.org.vn/images/fckUpload/image/HinhHanh/CIMSI-Thongtinvicuocsong.png" width="150" />';
			$content_new = str_replace($need_replace1, "", implode("", $matches[0]));
			$content_new = str_replace($need_replace2, "", $content_new);
			$pattern= "#<span id=\"lblNgayDuyet\">(.*)<\/span>#";
			$content_crawler = preg_replace($pattern,"",$content_new);			
			echo $content_crawler;			
		}
		die;
	}
	public function _get_images($content){
		 preg_match_all('/<img.+src=\"([^\"]+)\"[^\/>]+[\/]?>/i', $content, $images); 
		 return $image;
	}
	
	public function _get_links($content){
		preg_match_all("/<a.*?href=[\"'](\?action\=Detail.*[^\'])[\"']?.*?>(.*?)<\/a>/i", $content, $links);
		/*preg_match_all("'<a.*?href=\"(http[s]*://[^>\"]*?)\"[^>]*?>(.*?)</a>'si", $content, $links);*/
		return $links;
	}
	public function _get_links2($content){
		preg_match_all("/<a(?:[^>]*)href=\"([^\"]*)\"(?:[^>]*)title=\"([^\"]*)\">(?:[^<]*)<\/a>/is", $content, $links);
		/*preg_match_all("'<a.*?href=\"(http[s]*://[^>\"]*?)\"[^>]*?>(.*?)</a>'si", $content, $links);*/
		return $links;
	}	

	
	public function _get_class($content, $class_name=""){
		preg_match_all("/<div class=\"left\">(.*)\<\/div>/s", $content, $match);
		return $match;
	}			
}
